/*
 * **************************************************************************
 * *                                                                        *
 * * Ericsson hereby grants to the user a royalty-free, irrevocable,        *
 * * worldwide, nonexclusive, paid-up license to copy, display, perform,    *
 * * prepare and have prepared derivative works based upon the source code  *
 * * in this sample application, and distribute the sample source code and  *
 * * derivative works thereof and to grant others the foregoing rights.     *
 * *                                                                        *
 * * ERICSSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,        *
 * * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.       *
 * * IN NO EVENT SHALL ERICSSON BE LIABLE FOR ANY SPECIAL, INDIRECT OR      *
 * * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS    *
 * * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE  *
 * * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE *
 * * OR PERFORMANCE OF THIS SOFTWARE.                                       *
 * *                                                                        *
 * **************************************************************************
 */

package chat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.ericsson.nrgsdk.examples.tools.configuration.NestedProperties;
import com.ericsson.nrgsdk.examples.tools.configuration.NotFoundException;

/**
 * This class is responsible for parsing the configuration file and presenting
 * the configuration data using convenient data types.
 */
public class Configuration extends NestedProperties {

    /** The singleton instance that can be accessed by all users. */
	public static final Configuration INSTANCE = new Configuration();

	private Group[] itsGroups;

	private static String itsBillingInformation;

	/**
	 * Private constructor to prevent multiple instances.
	 */
	private Configuration() {
		//void
	}

	/**
	 * Initializes an instance.
	 * 
	 * @param aSource
	 *            the object who's package name will be used as name for the
	 *            .ini file.
	 * @exception IOException
	 *                if the file could not be loaded
	 * @exception NotFoundException
	 *                if an expected parameter could not be found
	 */
	public void load(Object aSource) throws IOException {
		super.load(aSource);
		//itsGroups = loadGroups("group");
		itsBillingInformation = loadString("billingInformation");
	}

	/**
	 * @return all groups.
	 */
//	public Group[] getGroups() {
//		return itsGroups;
//	}

//	private Group[] loadGroups(String aParm) {
//		List groups = new ArrayList();
//		for (int i = 0;; i++) {
//			String parm = aParm + "." + i;
//			try {
//				groups.add(loadGroup(parm));
//			} catch (NotFoundException e) {
//				if (prefixExists(parm)) {
//					throw e;
//				}
//				break;
//			}
//		}
//		return (Group[]) groups.toArray(new Group[0]);
//	}

//	private Group loadGroup(String aParm) {
//		Group group = new Group();
//		group.address = loadString(aParm + ".address");
//		group.members = loadMembers(aParm + ".member");
//		return group;
//	}

//	private String[] loadMembers(String aParm) {
//		List members = new ArrayList();
//		for (int i = 0;; i++) {
//			try {
//				members.add(loadString(aParm + "." + i));
//			} catch (NotFoundException e) {
//				break;
//			}
//		}
//		return (String[]) members.toArray(new String[0]);
//	}

	private String loadString(String aParm) {
		String s = getProperty(aParm);
		if (s == null) {
			throw new NotFoundException(aParm);
		}
		return s;
	}

	public String getBillingInformation() {
		return itsBillingInformation;
	}

	/**
	 * A convenience class for grouping Group-releated information
	 */
	public static class Group {

		public String address;

		public String[] members;

		public Integer assignmentId;

        public Group(String address, String[] members, Integer assignmentId) {
            this.address = address;
            this.members = members;
            this.assignmentId = assignmentId;
        }
                
                
	}
}
